﻿using Booking_System.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Booking_System.User_Controls
{
    /// <summary>
    /// Interaction logic for UCPhoneOverview.xaml
    /// </summary>
    public partial class UCPhoneOverview : UserControl
    {
        private Phone selPhone = null;

        public UCPhoneOverview()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            List<Phone> list = MainWindow.Service.GetAllPhones().ToList();

            PhoneItems diList = new PhoneItems();
            foreach (Phone p in list)
            {
                diList.Add(new Phone()
                {
                    Id = p.Id,
                    IsPrimary = p.IsPrimary,
                    Extension = p.Extension,
                    CountryCode = p.CountryCode,
                    AreaCode = p.AreaCode,
                    Number = p.Number,
                });
            }
            DataGrid1.ItemsSource = diList;
        }

        private void DataGrid1_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            selPhone = e.Row.Item as Phone;
            if (selPhone == null)
                return;
            if (e.EditAction == DataGridEditAction.Commit)
            {
                if (MainWindow.Service.UpdatePhone(selPhone))
                    MessageBox.Show("Phone with id: " + selPhone.Id + " updated!");
                else
                    MessageBox.Show("Couldn't update phone with id: " + selPhone.Id);
            }
        }

        public class PhoneItems : List<Phone> { }

        private void DeleteClick(object sender, RoutedEventArgs e)
        {
            if (selPhone == null)
                return;

            if (MainWindow.Service.DeletePhone(selPhone.Id))
                MessageBox.Show("Deleted phone with id: " + selPhone.Id + " updated!");
            else
                MessageBox.Show("Couldn't delete phone with id: " + selPhone.Id);
        }

        private void DataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            
        }
        /*public class DataItem
        {
            public int Id { get; set; }
            public Boolean IsPrimary { get; set; }
            public int Extension { get; set; }
            public int CountryCode { get; set; }
            public int AreaCode { get; set; }
            public int Number { get; set; }

        }*/
    }
}
